package com.ruoyi.project.product.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.oss.OSSUtils;
import com.ruoyi.project.product.domain.Products;
import com.ruoyi.project.product.domain.ProductsFile;
import com.ruoyi.project.product.service.ProductsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@RestController
@RequestMapping("/system/products")
public class ProductsController extends BaseController
{
    @Autowired
    private ProductsService productsService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Products zProducts)
    {
        startPage();
        List<Products> list = productsService.selectProductsList(zProducts);
        return getDataTable(list);
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Products products)
    {
        productsService.insertProducts(products);
        return AjaxResult.success();
    }

    /**
     * 上传文件
     */
    @PostMapping("/uploadFile")
    public AjaxResult uploadUserHead(MultipartFile[] file,Products products) throws IOException {
        for (MultipartFile f : file) {
            OSSUtils.putObject(f.getInputStream(),f.getOriginalFilename(),"abzy-video");
            String objectUrl = OSSUtils.getObjectUrl("abzy-video", f.getOriginalFilename());
            productsService.insertProductsFile(products,objectUrl,f.getOriginalFilename());
        }
        return AjaxResult.success();
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Products products)
    {
        List<Products> list = productsService.selectProductsList(products);
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(productsService.selectProductsByProductId(productId));
    }


    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Products products)
    {
        return toAjax(productsService.updateProducts(products));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(productsService.deleteProductsByProductIds(productIds));
    }

    /**
     * 删除附件
     * @param productsFileList
     * @return
     */
    @PostMapping("/delProductFiles")
    public AjaxResult delProductFiles( @RequestBody  List<ProductsFile> productsFileList)
    {
        productsService.delProductFiles(productsFileList);
        return AjaxResult.success();
    }
}
